package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateU8BaseDataDTO;
import com.itlong.cloud.POJO.PO.*;
import com.itlong.cloud.POJO.VO.operate.OperateU8BaseDataCustomerVO;
import com.itlong.cloud.POJO.VO.operate.OperateU8BaseDataDepartmentVO;
import com.itlong.cloud.POJO.VO.operate.OperateU8BaseDataSalesVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.datasource.dao.IU8SynchronizeBaseInfoDao;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateU8BaseDataDao;
import com.itlong.cloud.operate.service.IOperateU8BaseDataService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-U8基础数据管理业务处理接口实现类
 * </desc>
 *
 * @createDate 2020/12/02
 */
@Service
public class OperateU8BaseDataServiceImpl implements IOperateU8BaseDataService {

    @Autowired
    IOperateU8BaseDataDao iOperateU8BaseDataDao;

    @Autowired
    IU8SynchronizeBaseInfoDao iu8SynchronizeBaseInfoDao;


    /**
     * <desc>
     *      获取业务员部门列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2020/12/02
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateU8BaseDataDepartmentVO> getDepartmentPage(OperateU8BaseDataDTO dto) throws Exception {
        //分页类默认pageSize为15
        Page<OperateU8BaseDataDepartmentVO> page = new Page<>();
        page.getPage().setPageSize("15");
        page.getPage().setCurrentPage("1");
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto, page);
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateU8BaseDataDao.getDepartmentPage(params), iOperateU8BaseDataDao.getDepartmentPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取业务员列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2020/12/02
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateU8BaseDataSalesVO> getSalesPage(OperateU8BaseDataDTO dto) throws Exception {
        //分页类默认pageSize为15
        Page<OperateU8BaseDataSalesVO> page = new Page<>();
        page.getPage().setPageSize("15");
        page.getPage().setCurrentPage("1");
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto, page);
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateU8BaseDataDao.getSalesPage(params), iOperateU8BaseDataDao.getSalesPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取客户列表
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2020/12/02
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateU8BaseDataCustomerVO> getCustomerPage(OperateU8BaseDataDTO dto) throws Exception {
        //分页类默认pageSize为15
        Page<OperateU8BaseDataCustomerVO> page = new Page<>();
        page.getPage().setPageSize("15");
        page.getPage().setCurrentPage("1");
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto, page);
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateU8BaseDataDao.getCustomerPage(params), iOperateU8BaseDataDao.getCustomerPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      运营基础数据与U8同步定时任务
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2020/12/03
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public void timeTaskSynchronizeBaseDataByU8() throws Exception{
        Date nowTime = new Date();
        //从u8临时数据库查出任务书详情数据
        List<OperateU8AssignmentDetailsInfoPO> assignmentDetailsInfoByU8 = iu8SynchronizeBaseInfoDao.getAssignmentDetailsInfoByU8();
        if(!assignmentDetailsInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("1");
            iOperateU8BaseDataDao.batchSaveByAssignmentDetails(assignmentDetailsInfoByU8);
        }

        //从u8临时数据库查出任务书数据
        List<OperateU8AssignmentInfoPO> assignmentInfoByU8 = iu8SynchronizeBaseInfoDao.getAssignmentInfoByU8();
        if(!assignmentInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("2");
            iOperateU8BaseDataDao.batchSaveByAssignment(assignmentInfoByU8);
        }

        //从u8临时数据库查出客户数据
        List<OperateU8CustomerInfoPO> customerInfoByU8 = iu8SynchronizeBaseInfoDao.getCustomerInfoByU8();
        if(!customerInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("3");
            iOperateU8BaseDataDao.batchSaveByCustomer(customerInfoByU8);
        }

        //从u8临时数据库查出产品数据
        List<OperateU8ProductInfoPO> productInfoByU8 = iu8SynchronizeBaseInfoDao.getProductInfoByU8();
        if(!productInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("4");
            iOperateU8BaseDataDao.batchSaveByProduct(productInfoByU8);
        }

        //从u8临时数据库查出收货人数据
        List<OperateU8ReceiverInfoPO> receiverInfoByU8 = iu8SynchronizeBaseInfoDao.getReceiverInfoByU8();
        if(!receiverInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("5");
            iOperateU8BaseDataDao.batchSaveByReceiver(receiverInfoByU8);
        }

        //从u8临时数据库查出业务员部门数据
        List<OperateU8SaleDepartmentInfoPO> saleDepartmentInfoByU8 = iu8SynchronizeBaseInfoDao.getSaleDepartmentInfoByU8();
        if(!saleDepartmentInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("6");
            iOperateU8BaseDataDao.batchSaveByDepartment(saleDepartmentInfoByU8);
        }

        //从u8临时数据库查出业务员数据
        List<OperateU8SaleInfoPO> saleInfoByU8 = iu8SynchronizeBaseInfoDao.getSaleInfoByU8();
        if(!saleDepartmentInfoByU8.isEmpty()){
            //删除旧数据
            iOperateU8BaseDataDao.deleteOldData("7");
            iOperateU8BaseDataDao.batchSaveBySale(saleInfoByU8);
        }

        //获取项目与任务书关联列表(已筛选已保存的关联数据)
        List<OperateProjectAssignmentInfoPO> assignmentProjectList = iOperateU8BaseDataDao.getProjectAssignmentList();
        for (OperateProjectAssignmentInfoPO operateProjectAssignmentInfoPO : assignmentProjectList){
            operateProjectAssignmentInfoPO.setRelationId(LogicIdUtil.bussinessId());
            operateProjectAssignmentInfoPO.setCreateTime(nowTime);
            operateProjectAssignmentInfoPO.setUpdateTime(nowTime);
            operateProjectAssignmentInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
            operateProjectAssignmentInfoPO.setCreateUserId("sync");
            operateProjectAssignmentInfoPO.setOperateUserId("sync");
        }
        if (!assignmentProjectList.isEmpty()){
            iOperateU8BaseDataDao.batchSaveProjectAssignmentList(assignmentProjectList);
        }
     }
}
